<?php
	include_once("./dal/UserDAL.php");
	
	if(isLoggedIn())
	{
		header('Location: index.php');
	}
	
	$loginUsername = null;
	$loginPassword = null;
	
	$formIsValid = true;
	
	$loginSuccess = false;
	$loginAttempted = null;
	
	if(isset($_POST["btnLogin"]))
	{
		$loginAttempted = true;
		
		if($_POST["btnLogin"]=="Login")
		{		
			if(isset($_POST["txtUsername"]))
			{
				$loginUsername = trim($_POST["txtUsername"]);
			}

			if(isset($_POST["txtPassword"]))
			{
				$loginPassword = trim($_POST["txtPassword"]);
			}			
			
			$formIsValid = true;
			
			if($formIsValid)
			{
				$result = loginUser($loginUsername, $loginPassword);
				
				$loginSuccess = $result;
				
				if($loginSuccess===true)
				{
					$_SESSION['username'] = $loginUsername;
					$date = date_create();
					$_SESSION['logintime'] = date_timestamp_get($date);
					
					header('Location: index.php');
					exit;					
				}
			}
		}
	}

	function getContent()
	{
		global 
		$loginUsername, 
		$loginPassword,
		$formIsValid,
		$loginSuccess,
		$loginAttempted;
		
		include_once('./templates/LoginForm.php'); 	
	}
?>